package com.qf.manager.controller;

import com.qf.manager.pojo.dto.ProductsItems;
import com.qf.manager.pojo.po.Product;
import com.qf.manager.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductsServlet {
    @Autowired
    private Service service;

    @ResponseBody
    @RequestMapping(value = "/item/list")
    public ProductsItems listProduct(Map<String,Object> map, @RequestParam("page") Long page, @RequestParam("limit")Long limit){

        ProductsItems pi =new ProductsItems();

        pi.setCode(0);

        pi.setMessage("success");

        List<Product> data = service.selectProduct(page, limit);

        pi.setData(data);

        int count = service.selectCount();

        pi.setCount(count);

        return pi;
    }


   @RequestMapping(value = "item/del",method = RequestMethod.POST)
    public void dProduct(String pid, HttpServletResponse response) throws IOException {


        System.out.println("11111");

        boolean result =false;

       result = service.delProduct(pid);
        if(result){
            response.getWriter().write("true");

        }else{

            response.getWriter().write("false");
        }


    }

    @RequestMapping(value = "/item/edit1")
    @ResponseBody
    public Map<String,Integer> editProduct(String pid, HttpServletRequest request){
        System.out.println(pid);
        Map<String,Integer> map =new HashMap<>();
        Integer i=0;
        try {

            Product product1 = service.selectProductByPid(pid);


           i = service.editProduct(product1);


        } catch (Exception e) {
            e.printStackTrace();
        }

        map.put("result",i);


        return map;
    }
    @RequestMapping("/item/edit2")
    @ResponseBody
    public Product editReview(String pid){


        Product product1 = null;
        try {
            product1 = service.selectProductByPid(pid);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return product1;
    }
    @RequestMapping("/item/update")
    @ResponseBody
    public Map<String,String> updateProduct(Product product){
        Map<String,String>  map = new HashMap<>();

        if(product.getPid()!=null){
            service.updateProduct(product);

        }

        return map;
    }


}
